package com.guan.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.guan.commonutils.R;
import com.guan.servicebase.exceptonhandler.GuliException;
import com.guan.vod.service.VodService;
import com.guan.vod.util.ConstantVodUtil;
import com.guan.vod.util.InitVodClient;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video/*")
@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;

    /**
     * 上传视频到aliyun
     */
    @PostMapping("uplodeAliyunVideo")
    public R uplodeAliyunVideo(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestParam("file") MultipartFile file)  throws Exception {
        String videoId = vodService.uplodeAliyunVideo(file);
        return R.ok().message("视频上传成功").data("videoId",videoId);
    }

    /**
     * @date: 2023/3/9 11:01
     * @author: 管奇安
     * @description: 通过视频id删除视频
     * @param: [id] 视频id
     * @return: com.guan.commonutils.R
     **/
    @DeleteMapping("removeAliyunVideo/{id}")
    public R removeAliyunVideo(@PathVariable String id){

        try {
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtil.KEY_ID,
                    ConstantVodUtil.KEY_SECRET);
            //创建删除视频的request对象
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
            //向request设置视频id
            deleteVideoRequest.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(deleteVideoRequest);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败！");
        }
    }
    
    /**
     * @date: 2023/3/9 11:07
     * @author: 管奇安
     * @description: 批量删除阿里云视频
     * @param: [videoIdList]
     * @return: com.guan.commonutils.R
     **/
    @DeleteMapping("delete-batch")
    public R deleteBatch(@ApiParam(name = "videoIdList",value = "云端视频ID",required = true)
                             @RequestParam("videoIdList")List<String> videoIdList){
        
        vodService.removeVideoList(videoIdList);
        return R.ok().message("删除成功！");
    }

    /**
     * @date: 2023/4/24 14:47
     * @author: 管奇安
     * @description: 更具视频id获取视频凭证
     * @param: [id] 视频id
     * @return: com.guan.commonutils.R
     **/
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id){

        try {
            //创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtil.KEY_ID,ConstantVodUtil.KEY_SECRET);
            //创建获取凭证的request response 对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request中设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();

            return R.ok().data("playAuth",playAuth);

        }catch (Exception e){
            throw new GuliException(20001,"获取视频凭证失败");
        }
    }

}
